
package net.lucamasini.fantacalcio.gwt.client.view;

import static net.lucamasini.fantacalcio.gwt.client.Fantacalcio.getEventBus;
import net.lucamasini.fantacalcio.gwt.client.event.ClassificaEvent;
import net.lucamasini.fantacalcio.gwt.client.event.CreaPronosticoEvent;
import net.lucamasini.fantacalcio.gwt.client.event.NuovaGiornataEvent;
import net.lucamasini.fantacalcio.server.model.Partita;

import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.view.client.HasData;

public class AggiornaRisultatiPanel extends Composite implements AggiornaRisultatiView {
    interface Binder extends UiBinder<Widget, AggiornaRisultatiPanel> {
    }

    private static final Binder BINDER = GWT.create(Binder.class);

    public AggiornaRisultatiPanel() {
        initWidget(BINDER.createAndBindUi(this));
        
        partiteCellTable.addColumn(new TextColumn<Partita>() {
			@Override
			public String getValue(Partita partita) {
				return partita.getCasa();
			}
		}, "PARTITA");

        partiteCellTable.addColumn(new TextColumn<Partita>() {
			@Override
			public String getValue(Partita partita) {
				return partita.getTrasferta();
			}
		});

        final Column<Partita, String> casa = new Column<Partita, String>(
				new EditTextCell()) {
        	{
        		setFieldUpdater(new FieldUpdater<Partita, String>() {

        			@Override
        			public void update(int index, Partita object, String value) {

    					object.getRisultato().setCasa(Integer.parseInt(value));	
        			}
        		});
        	}
			@Override
			public String getValue(Partita object) {
				return Integer.toString(object.getRisultato().getCasa());
			}
		};
		
        final Column<Partita, String> trasferta = new Column<Partita, String>(
				new EditTextCell()) {
        	{
        		setFieldUpdater(new FieldUpdater<Partita, String>() {

        			@Override
        			public void update(int index, Partita object, String value) {

        				object.getRisultato().setTrasferta(Integer.parseInt(value));
        			}
        		});
        	}
			@Override
			public String getValue(Partita object) {
				return Integer.toString(object.getRisultato().getTrasferta());
			}
		};

		partiteCellTable.addColumn(casa, "RISULTATO");
        partiteCellTable.addColumn(trasferta);
    }

    @UiField Label descrizioneGiornata; 
    @UiField Label dataGiornata; 
    @UiField CellTable<Partita> partiteCellTable;
    @UiField Button salvaButton;
    @UiField Button consolidaButton;

    @UiField Hyperlink classifica;
    @UiField Hyperlink nuovaGiornata;
    
    @UiHandler("nuovaGiornata")
    void nuovaGiornataClicked(ClickEvent event) {
    	getEventBus().fireEvent(new NuovaGiornataEvent());
    }

    @UiHandler("classifica")
    void classificaClicked(ClickEvent event) {
    	getEventBus().fireEvent(new ClassificaEvent());
    }

    @UiHandler("salvaButton")
    void confermaClicked(ClickEvent event) {
    	getEventBus().fireEvent(new CreaPronosticoEvent());
    }

	@Override
	public HasData<Partita> partite() {
		return partiteCellTable;
	}

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public void setName(String name) {
        throw new UnsupportedOperationException("setName not support yet in "+this.getClass().getName());
    }

    @Override
    public String getName() {
        return "Crea Intervento Panel";
    }


	@Override
	public HasText descrizioneGiornata() {
		return descrizioneGiornata;
	}


	@Override
	public HasText dataGiornata() {
		return dataGiornata;
	}


}
